package com.tchintech.pay.utils;

import android.content.Context;
import android.os.SystemClock;
import android.widget.Toast;

import com.zng.common.PrintPowerOnAndOffUtils;
import com.zng.common.PrintUtils;
import com.zng.common.init.InitExtendFunction;

/**
 * Created by Administrator on 2017/9/21 0021.
 */

public class PrintUtil {

    private PrintUtils mPrintUtils;
    private PrintPowerOnAndOffUtils mPrintPowerOnAndOffUtils;
    private boolean isPowerOn;// 是否上电
    private Context context;
    private String ptintC = "";
    //存根
    private String[] cg = {"客户存根", "商户存根"};

    public PrintUtil(Context context, String[] content) {
        this.context = context;
        mPrintUtils = new PrintUtils(context);
        mPrintPowerOnAndOffUtils = new PrintPowerOnAndOffUtils(context);
        mPrintPowerOnAndOffUtils.powerOn();
        setStr(content);
        System.out.println("okhttp初始化" );
    }

    public void setStr(String[] content) {

        String[] name = {"", "================================", "商户名称", "商户编号", "终端编号", "交易类型", "支付类型", "订单号", "流水号", "交易日期时间", "金额", "======================"};
        //String[] num = {"POS签购单", "", "西安市雁塔区", "123456789", "963852", "二维码下单", "支付宝支付", "12345678946549879", "00348", "2017/09/21 14:32:22", "RMB  2160.00", "备注："};
         String model = android.os.Build.MODEL;
        System.out.println("okhttp当前的型号" + model);
        if ("POS-L12".contains("POS")) {
            SystemClock.sleep(300);//上电成功后加300毫秒延时
            startPrint(name, content, 0);
        }
    }

    public void startPrint(final String[] name, final String[] content, final int cungen) {
        isPowerOn = mPrintPowerOnAndOffUtils.checkPowerStatus();
        if (isPowerOn) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    int ret = mPrintUtils.detectionPrinter();
                    mPrintUtils.setLineSpace(40);//行间距为3/203英寸 * 25.4 = 0.375mm
                    if (ret == 0) {
                        for (int i = 0; i < name.length; i++) {
                            if (!name[i].equals("")) {
                                //取消倍高倍宽打印
                                mPrintUtils.setCharFormatNormal();
                                mPrintUtils.printText(name[i]);
                                mPrintUtils.printNewLine();
                            }
                            if (!content[i].equals("")) {
                                if (i == 0) {
                                    //剧中打印
                                    mPrintUtils.printerSetLineAlign(1);
                                }
                                //设置部分数据倍高倍宽打印
                                if (i == 0 || i == 2 || i == 5 || i == 9 || i == 10) {
                                    mPrintUtils.setCharDoubleWidth();
                                    mPrintUtils.setCharDoubleHeight();
                                }
                                mPrintUtils.printText(content[i]);
                                mPrintUtils.printNewLine();
                                //剧左对齐
                                mPrintUtils.printerSetLineAlign(0);
                            }
                        }
                        mPrintUtils.printStepPass(6);
                        mPrintUtils.printText(cg[cungen]);
                        //一次打印2连
                        if (cungen < 1) {
                            startPrint(name, content, cungen + 1);
                        }
                        mPrintUtils.printStepPass(4);
                    } else if (ret == -1) {
                        InitExtendFunction.extendFunctionInit();
                    }
                }
            }).start();
        } else {
            Toast.makeText(context, "请先上电", Toast.LENGTH_SHORT).show();
        }
    }
}
